package cn.tedu.store.controller;

import cn.tedu.store.entity.Order;
import cn.tedu.store.entity.UserEntity;
import cn.tedu.store.service.IOrderService;
import cn.tedu.store.util.JsonResult;
import com.netflix.client.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.resource.HttpResource;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    IOrderService service;

    @RequestMapping("/create")
    public JsonResult<Void> createOrder(Integer aid, Integer[] cids,
                                        Integer uid, String username,
                                        HttpSession session,
                                        HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
//        UserEntity user=(UserEntity)session.getAttribute("user");
        service.createOrder(aid,cids,uid,username);
        return JsonResult.getSuccessJR();
    }

    @RequestMapping("/changeStatus")
    public JsonResult<Void> changeStatus(Integer oid,
                                        Integer status,
                                        HttpSession session,
                                        HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
//        UserEntity user=(UserEntity)session.getAttribute("user");
        service.changeStatus(oid, status);
        return JsonResult.getSuccessJR();
    }

    @RequestMapping("/list")
    public JsonResult<List<Order>> findOrder( Integer uid,HttpSession session,
                                              HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
//        UserEntity user=(UserEntity)session.getAttribute("user");
        List<Order> data=service.findByUid(uid);
        return JsonResult.getSuccessJR(data);
    }

    @RequestMapping("/listWithStatus")
    public JsonResult<List<Order>> findOrderWithStatus( Integer uid,Integer status,HttpSession session,
                                              HttpServletResponse resp){
        resp.setHeader("Access-Control-Allow-Origin", "*");
//        UserEntity user=(UserEntity)session.getAttribute("user");
        List<Order> data=service.findByUidAndStatus(uid,status);
        return JsonResult.getSuccessJR(data);
    }
}
